package com.smrs.goods.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.goods.model.GoodsAttributeModel;
import com.smrs.goods.model.GoodsAttributeValueModel;

public class GoodsAttributeValueDao extends BaseDAOHibernateImpl<GoodsAttributeValueModel, Long>{
	public List<GoodsAttributeValueModel> getGoodsAttributeValueByAttribute(GoodsAttributeModel attribute){
		String hsql = " from GoodsAttributeValueModel where goodsAttribute.id="+attribute.getId();
		Query query=this.createHQLQuery(hsql);
		List<GoodsAttributeValueModel> list=query.list();
		return list;
	} 
	
	public Pager<GoodsAttributeValueModel> findByGoodsAttributeValue(GoodsAttributeValueModel valueModel,Pager<GoodsAttributeValueModel> pageRequest){
		
		String hsql=" from GoodsAttributeValueModel where 1=1";
		
		if(StringUtils.isNotEmpty(valueModel.getName())){
		   hsql = hsql+" and  name like '%"+valueModel.getName()+"%'"; 
		}
		if(valueModel.getGoodsAttribute()!=null && valueModel.getGoodsAttribute().getId()!=null){
			hsql=hsql+ " and goodsAttribute.id=" + valueModel.getGoodsAttribute().getId();
		}
		Pager<GoodsAttributeValueModel> result = Pager.cloneFromPager(pageRequest);
		Long totalCount=countHQLUniqueResule(hsql,new Object[]{});
		result.setTotalRecords(totalCount);		
		Query query = this.getSession().createQuery(hsql);
		this.setPageParameterToQuery(query, pageRequest);
		result.setRecords(query.list());
		return result;		
		
	}
	
}
